package com.yyysj.friend.service.Impl;

import com.yyysj.common.core.constants.Constants;
import com.yyysj.common.core.utils.ThreadLocalUtils;
import com.yyysj.friend.domain.dto.PostAddDTO;
import com.yyysj.friend.domain.po.Post;
import com.yyysj.friend.domain.vo.PostAddVO;
import com.yyysj.friend.domain.vo.PostListVO;
import com.yyysj.friend.mapper.PostMapper;
import com.yyysj.friend.service.IPostService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
@RequiredArgsConstructor
public class PostServiceImpl implements IPostService {

    private final PostMapper postMapper;

    @Override
    public PostListVO list() {
        // 从数据库中查询数据
        List<Post> postList = postMapper.selectList(null);
        PostListVO postListVO = new PostListVO();
        postListVO.setPostList(postList);
        postListVO.setTotal(0L);
        return postListVO;
    }

    @Override
    public PostAddVO add(PostAddDTO postAddDTO) {

        Post post = new Post();

        // 赋值标题和内容
        post.setTitle(postAddDTO.getTitle());
        post.setContent(postAddDTO.getContent());
        // 获取当前登录用户的ID
        Long userId = ThreadLocalUtils.get(Constants.USER_ID, Long.class);
        post.setUserId(userId);

        postMapper.insert(post);

        PostAddVO postAddVO = new PostAddVO();
        postAddVO.setTitle(post.getTitle());
        postAddVO.setContent(post.getContent());

        return postAddVO;
    }
}
